//
//  GFCalendarScrollView.m
//
//  Created by Mercy on 2016/11/9.
//  Copyright © 2016年 Mercy. All rights reserved.
//

#import "GFCalendarScrollView.h"
#import "GFCalendarCell.h"
#import "GFCalendarMonth.h"
#import "NSDate+GFCalendar.h"
#import "UIColor+CL.h"
@interface GFCalendarScrollView() <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionViewL;
@property (nonatomic, strong) UICollectionView *collectionViewM;
@property (nonatomic, strong) UICollectionView *collectionViewR;

@property (nonatomic, strong) NSDate *currentMonthDate;

@property (nonatomic, strong) NSMutableArray *monthArray;
@property (nonatomic,copy)NSString *todayNum;

@end

@implementation GFCalendarScrollView

#define kCalendarBasicColor [UIColor colorWithRed:231.0 / 255.0 green:85.0 / 255.0 blue:85.0 / 255.0 alpha:1.0]
//#define kCalendarBasicColor [UIColor colorWithRed:252.0 / 255.0 green:60.0 / 255.0 blue:60.0 / 255.0 alpha:1.0]

static NSString *const kCellIdentifier = @"cell";


#pragma mark - Initialiaztion

- (instancetype)initWithFrame:(CGRect)frame {
    
    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.delegate = self;
        
        self.contentSize = CGSizeMake(3 * self.bounds.size.width, self.bounds.size.height);
        [self setContentOffset:CGPointMake(self.bounds.size.width, 0.0) animated:NO];
        
        _currentMonthDate = [NSDate date];
//         [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dateArr:) name:@"date" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sele:) name:@"sele" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getdate:) name:@"dateArr" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setCurrentdate:) name:@"Currentdate" object:nil];
        [self setupCollectionViews];
        
        
    }
    
    return self;
    
}

-(void)setCurrentdate:(NSNotification *)n{
    NSDate *date0 = [n object];
    _currentMonthDate = date0;
}

-(void)getdate:(NSNotification *)n{
    NSArray *arr = [n object];
    _dateArr = arr;
    [_collectionViewM reloadData]; // 中间的 collectionView 先刷新数据
    [_collectionViewL reloadData]; // 最后两边的 collectionView 也刷新数据
    [_collectionViewR reloadData];
    
}



-(void)sele:(NSNotification *)n{
    [_monthArray removeAllObjects];
    NSArray *arr = [n object];
    [_monthArray addObjectsFromArray:[n object]];
    // 刷新数据
    [_collectionViewM reloadData];
    [_collectionViewL reloadData];
    [_collectionViewR reloadData];
}

- (NSMutableArray *)monthArray {
    
    if (_monthArray == nil) {
        
        _monthArray = [NSMutableArray arrayWithCapacity:4];
        
        NSDate *previousMonthDate = [_currentMonthDate previousMonthDate];
        NSDate *nextMonthDate = [_currentMonthDate nextMonthDate];
        
        [_monthArray addObject:[[GFCalendarMonth alloc] initWithDate:previousMonthDate]];
        [_monthArray addObject:[[GFCalendarMonth alloc] initWithDate:_currentMonthDate]];
        [_monthArray addObject:[[GFCalendarMonth alloc] initWithDate:nextMonthDate]];
        [_monthArray addObject:[self previousMonthDaysForPreviousDate:previousMonthDate]]; // 存储左边的月份的前一个月份的天数，用来填充左边月份的首部
        
        // 发通知，更改当前月份标题
        [self notifyToChangeCalendarHeader];
    }
    
    return _monthArray;
}

- (NSNumber *)previousMonthDaysForPreviousDate:(NSDate *)date {
    return [[NSNumber alloc] initWithInteger:[[date previousMonthDate] totalDaysInMonth]];
}


- (void)setupCollectionViews {
        
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.bounds.size.width / 7.0, 61);
    flowLayout.minimumLineSpacing = 0.0;
    flowLayout.minimumInteritemSpacing = 0.0;
    
    CGFloat selfWidth = self.bounds.size.width;
    CGFloat selfHeight = self.bounds.size.height;
    
    _collectionViewL = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0, 0.0, selfWidth, selfHeight) collectionViewLayout:flowLayout];
    _collectionViewL.dataSource = self;
    _collectionViewL.delegate = self;
    _collectionViewL.backgroundColor = [UIColor clearColor];
    [_collectionViewL registerClass:[GFCalendarCell class] forCellWithReuseIdentifier:kCellIdentifier];
    [self addSubview:_collectionViewL];
    _collectionViewL.backgroundColor =[UIColor clearColor];

    
    _collectionViewM = [[UICollectionView alloc] initWithFrame:CGRectMake(selfWidth, 0.0, selfWidth, selfHeight) collectionViewLayout:flowLayout];
    _collectionViewM.dataSource = self;
    _collectionViewM.delegate = self;
    _collectionViewM.backgroundColor = [UIColor clearColor];
    [_collectionViewM registerClass:[GFCalendarCell class] forCellWithReuseIdentifier:kCellIdentifier];
    [self addSubview:_collectionViewM];
    _collectionViewM.backgroundColor =[UIColor clearColor];
    
    _collectionViewR = [[UICollectionView alloc] initWithFrame:CGRectMake(2 * selfWidth, 0.0, selfWidth, selfHeight) collectionViewLayout:flowLayout];
    _collectionViewR.dataSource = self;
    _collectionViewR.delegate = self;
    _collectionViewR.backgroundColor = [UIColor clearColor];
    [_collectionViewR registerClass:[GFCalendarCell class] forCellWithReuseIdentifier:kCellIdentifier];
    [self addSubview:_collectionViewR];
    _collectionViewR.backgroundColor =[UIColor clearColor];

}


#pragma mark -

- (void)notifyToChangeCalendarHeader {
    
    GFCalendarMonth *currentMonthInfo = self.monthArray[1];
    NSLog(@"~~~%ld-%ld",(long)currentMonthInfo.year,(long)currentMonthInfo.month);
    
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:2];
    
    [userInfo setObject:[[NSNumber alloc] initWithInteger:currentMonthInfo.year] forKey:@"year"];
    [userInfo setObject:[[NSNumber alloc] initWithInteger:currentMonthInfo.month] forKey:@"month"];
    
    NSNotification *notify = [[NSNotification alloc] initWithName:@"ChangeCalendarHeaderNotification" object:nil userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notify];
}

- (void)refreshToCurrentMonth {
    
    // 如果现在就在当前月份，则不执行操作
    GFCalendarMonth *currentMonthInfo = self.monthArray[1];
    if ((currentMonthInfo.month == [[NSDate date] dateMonth]) && (currentMonthInfo.year == [[NSDate date] dateYear])) {
        return;
    }
    
    _currentMonthDate = [NSDate date];
    
    NSDate *previousMonthDate = [_currentMonthDate previousMonthDate];
    NSDate *nextMonthDate = [_currentMonthDate nextMonthDate];
    
    [self.monthArray removeAllObjects];
    [self.monthArray addObject:[[GFCalendarMonth alloc] initWithDate:previousMonthDate]];
    [self.monthArray addObject:[[GFCalendarMonth alloc] initWithDate:_currentMonthDate]];
    [self.monthArray addObject:[[GFCalendarMonth alloc] initWithDate:nextMonthDate]];
    [self.monthArray addObject:[self previousMonthDaysForPreviousDate:previousMonthDate]];
    
    // 刷新数据
    [_collectionViewM reloadData];
    [_collectionViewL reloadData];
    [_collectionViewR reloadData];
    
}



#pragma mark - UICollectionDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 42; // 7 * 6
}

- (NSString *)calculateWeek:(NSDate *)date{
    //计算week数
    NSCalendar * myCalendar = [NSCalendar currentCalendar];
    myCalendar.timeZone = [NSTimeZone systemTimeZone];
    NSInteger week = [[myCalendar components:NSWeekdayCalendarUnit fromDate:date] weekday];
    NSLog(@"week : %zd",week);
    switch (week) {
        case 1:
        {
            return @"周日";
        }
        case 2:
        {
            return @"周一";
        }
        case 3:
        {
            return @"周二";
        }
        case 4:
        {
            return @"周三";
        }
        case 5:
        {
            return @"周四";
        }
        case 6:
        {
            return @"周五";
        }
        case 7:
        {
            return @"周六";
        }
    }
    
    return @"";
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GFCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
//    cell.todayCircleImg.backgroundColor =[UIColor colorWithRed:234/255.0 green:107/255.0 blue:99/255.0 alpha:1];
    cell.biankuang.hidden = YES;
    cell.todayIcon.hidden = YES;
    cell.bottomImg.hidden = YES;
    if (collectionView == _collectionViewL) {
        
        GFCalendarMonth *monthInfo = self.monthArray[0];
        NSInteger firstWeekday = monthInfo.firstWeekday;
        NSInteger totalDays = monthInfo.totalDays;
        
        // 当前月
        if (indexPath.row >= firstWeekday && indexPath.row < firstWeekday + totalDays) {
            cell.biankuang.hidden = NO;
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday + 1];
            NSString *hh = @"0";
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
            int date = [cell.todayLabel.text intValue];
            NSString *dateStr =(date<10)?[NSString stringWithFormat:@"%@%d",hh,date]:[NSString stringWithFormat:@"%d",date];
            
            NSString *monthStr =(monthInfo.month<10)?[NSString stringWithFormat:@"%@%ld",hh,(long)monthInfo.month]:[NSString stringWithFormat:@"%ld",(long)monthInfo.month];
            
            NSLog(@"%ld-%@-%@",(long)monthInfo.year,monthStr,dateStr);//本月日期打印
            NSString *lastDate = [NSString stringWithFormat:@"%ld-%@-%@",(long)monthInfo.year,monthStr,dateStr];
            NSString *compar =[NSString stringWithFormat:@"conmpare %ld--%ld",(long)monthInfo.month,(long)[[NSDate date] dateMonth]];
            NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
            [formatter1 setDateFormat:@"yyyy-MM-dd"];
            
            NSDate *resDate = [formatter1 dateFromString:lastDate];
            
            NSString *xingqi =[self calculateWeek:resDate];
            if([xingqi isEqualToString:@"周日"]||[xingqi isEqualToString:@"周六"]){
                cell.todayLabel.textColor = [UIColor colorWithHex:0xfb5a1b];
            }else{
                cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];
            }
            

            NSLog(@"ttt%@-%@",xingqi,lastDate);
//            NSLog(@"cddo---%@",compar);
//            cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];
            if(_dateArr.count>0){
                for(NSDictionary *dic in _dateArr){
                    if ([lastDate isEqualToString:[dic objectForKey:@"date"]]) {
                        cell.bottomImg.hidden = NO;
                        cell.bottomImg.image =[UIImage imageNamed:@"jihuaok"];
                    }
                }
            }
            
            
            
           
            
            // 标识今天
            if ((monthInfo.month == [[NSDate date] dateMonth]) && (monthInfo.year == [[NSDate date] dateYear])) {
                if (indexPath.row == [[NSDate date] dateDay] + firstWeekday - 1) {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];
                    cell.todayLabel.textColor = [UIColor whiteColor];
                    cell.todayIcon.hidden = NO;
                
                } else {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];
                }
            } else {
                cell.todayCircle.backgroundColor = [UIColor clearColor];
            }
            
        }
        // 补上前后月的日期，淡色显示
        else if (indexPath.row < firstWeekday) {
            int totalDaysOflastMonth = [self.monthArray[3] intValue];
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", totalDaysOflastMonth - (firstWeekday - indexPath.row) + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        } else if (indexPath.row >= firstWeekday + totalDays) {
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday - totalDays + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        }
        
        cell.userInteractionEnabled = NO;
        
    }
    else if (collectionView == _collectionViewM) {
        
        GFCalendarMonth *monthInfo = self.monthArray[1];
        NSInteger firstWeekday = monthInfo.firstWeekday;
        NSInteger totalDays = monthInfo.totalDays;
        
        // 当前月
        if (indexPath.row >= firstWeekday && indexPath.row < firstWeekday + totalDays) {
            cell.biankuang.hidden = NO;
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday + 1];
            NSString *hh = @"0";
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
            int date = [cell.todayLabel.text intValue];
            NSString *dateStr =(date<10)?[NSString stringWithFormat:@"%@%d",hh,date]:[NSString stringWithFormat:@"%d",date];
            
             NSString *monthStr =(monthInfo.month<10)?[NSString stringWithFormat:@"%@%ld",hh,(long)monthInfo.month]:[NSString stringWithFormat:@"%ld",(long)monthInfo.month];
            
            NSLog(@" hahahah %ld-%@-%@",(long)monthInfo.year,monthStr,dateStr);//本月日期打印
            NSString *lastDate = [NSString stringWithFormat:@"%ld-%@-%@",(long)monthInfo.year,monthStr,dateStr];
            NSString *compar =[NSString stringWithFormat:@"conmpare %ld--%ld",(long)monthInfo.month,(long)[[NSDate date] dateMonth]];
            
            NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
            [formatter1 setDateFormat:@"yyyy-MM-dd"];
            
            NSDate *resDate = [formatter1 dateFromString:lastDate];
            
            NSString *xingqi =[self calculateWeek:resDate];
            if([xingqi isEqualToString:@"周日"]||[xingqi isEqualToString:@"周六"]){
                cell.todayLabel.textColor = [UIColor colorWithHex:0xfb5a1b];
            }else{
                cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];
            }
            NSLog(@"bbb%@-%@",xingqi,lastDate);
            NSLog(@"cddo---%@",compar);
            
//            cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];
            if(_dateArr.count>0){
                for(NSDictionary *dic in _dateArr){
                    if ([lastDate isEqualToString:[dic objectForKey:@"date"]]) {
                        cell.bottomImg.hidden = NO;
                        cell.bottomImg.image =[UIImage imageNamed:@"jihuaok"];
                    }
                }
            }
            
            
            cell.userInteractionEnabled = YES;
            
            // 标识今天
            if ((monthInfo.month == [[NSDate date] dateMonth]) && (monthInfo.year == [[NSDate date] dateYear])) {
                if (indexPath.row == [[NSDate date] dateDay] + firstWeekday - 1) {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];
                    cell.todayLabel.textColor = [UIColor whiteColor];
                    cell.todayIcon.hidden = NO;
                            } else {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];
                }
            } else {
                cell.todayCircle.backgroundColor = [UIColor clearColor];
            }
            
        }
        // 补上前后月的日期，淡色显示
        else if (indexPath.row < firstWeekday) {
            GFCalendarMonth *lastMonthInfo = self.monthArray[0];
            NSInteger totalDaysOflastMonth = lastMonthInfo.totalDays;
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", totalDaysOflastMonth - (firstWeekday - indexPath.row) + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.userInteractionEnabled = NO;
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        } else if (indexPath.row >= firstWeekday + totalDays) {
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday - totalDays + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.userInteractionEnabled = NO;
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        }
        
    }
    else if (collectionView == _collectionViewR) {
        
        GFCalendarMonth *monthInfo = self.monthArray[2];
        NSInteger firstWeekday = monthInfo.firstWeekday;
        NSInteger totalDays = monthInfo.totalDays;
        
        // 当前月
        if (indexPath.row >= firstWeekday && indexPath.row < firstWeekday + totalDays) {
            cell.biankuang.hidden = NO;
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday + 1];
            
            NSString *hh = @"0";
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
            int date = [cell.todayLabel.text intValue];
            NSString *dateStr =(date<10)?[NSString stringWithFormat:@"%@%d",hh,date]:[NSString stringWithFormat:@"%d",date];
            
            NSString *monthStr =(monthInfo.month<10)?[NSString stringWithFormat:@"%@%ld",hh,(long)monthInfo.month]:[NSString stringWithFormat:@"%ld",(long)monthInfo.month];
            
            NSLog(@"%ld-%@-%@",(long)monthInfo.year,monthStr,dateStr);//本月日期打印
            NSString *lastDate = [NSString stringWithFormat:@"%ld-%@-%@",(long)monthInfo.year,monthStr,dateStr];
            NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
            [formatter1 setDateFormat:@"yyyy-MM-dd"];
            
            NSDate *resDate = [formatter1 dateFromString:lastDate];
            
            NSString *xingqi =[self calculateWeek:resDate];
            if([xingqi isEqualToString:@"周日"]||[xingqi isEqualToString:@"周六"]){
                cell.todayLabel.textColor = [UIColor colorWithHex:0xfb5a1b];
            }else{
                cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];
            }
//            cell.todayLabel.textColor = [UIColor colorWithHex:0x434343];

            if(_dateArr.count>0){
                for(NSDictionary *dic in _dateArr){
                    if ([lastDate isEqualToString:[dic objectForKey:@"date"]]) {
                        cell.bottomImg.hidden = NO;
                        cell.bottomImg.image =[UIImage imageNamed:@"jihuaok"];
                    }
                }
            }
            
                  // 标识今天
            if ((monthInfo.month == [[NSDate date] dateMonth]) && (monthInfo.year == [[NSDate date] dateYear])) {
                if (indexPath.row == [[NSDate date] dateDay] + firstWeekday - 1) {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];//今天的颜色
                    cell.todayLabel.textColor = [UIColor whiteColor];
                    cell.todayIcon.hidden = NO;
            
                } else {
                    cell.todayCircle.backgroundColor = [UIColor clearColor];
                }
            } else {
                cell.todayCircle.backgroundColor = [UIColor clearColor];
            }
            
        }
        // 补上前后月的日期，淡色显示
        else if (indexPath.row < firstWeekday) {
            GFCalendarMonth *lastMonthInfo = self.monthArray[1];
            NSInteger totalDaysOflastMonth = lastMonthInfo.totalDays;
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", totalDaysOflastMonth - (firstWeekday - indexPath.row) + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        } else if (indexPath.row >= firstWeekday + totalDays) {
            cell.todayLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row - firstWeekday - totalDays + 1];
            cell.todayLabel.textColor = [UIColor clearColor];
            cell.todayCircle.backgroundColor = [UIColor clearColor];
            cell.todayCircleImg.image =[UIImage imageNamed:@""];
            cell.num.text = @"";
        }
        
        cell.userInteractionEnabled = NO;
        
    }
    
    return cell;
    
}


#pragma mark - UICollectionViewDeleagate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.didSelectDayHandler != nil) {
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth) fromDate:_currentMonthDate];
        NSDate *currentDate = [calendar dateFromComponents:components];
        
        GFCalendarCell *cell = (GFCalendarCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        NSInteger year = [currentDate dateYear];
        NSInteger month = [currentDate dateMonth];
        NSInteger day = [cell.todayLabel.text integerValue];
//        cell.todayCircle.backgroundColor =[UIColor redColor];
        
        self.didSelectDayHandler(year, month, day); // 执行回调
    }
    
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView != self) {
        return;
    }
    
    
    // 向右滑动
    if (scrollView.contentOffset.x < self.bounds.size.width) {
        
        _currentMonthDate = [_currentMonthDate previousMonthDate];
        NSDate *previousDate = [_currentMonthDate previousMonthDate];
        
        // 数组中最左边的月份现在作为中间的月份，中间的作为右边的月份，新的左边的需要重新获取
        GFCalendarMonth *currentMothInfo = self.monthArray[0];
        GFCalendarMonth *nextMonthInfo = self.monthArray[1];
        
        
        GFCalendarMonth *olderNextMonthInfo = self.monthArray[2];
        
        // 复用 GFCalendarMonth 对象
        olderNextMonthInfo.totalDays = [previousDate totalDaysInMonth];
        olderNextMonthInfo.firstWeekday = [previousDate firstWeekDayInMonth];
        olderNextMonthInfo.year = [previousDate dateYear];
        olderNextMonthInfo.month = [previousDate dateMonth];
        GFCalendarMonth *previousMonthInfo = olderNextMonthInfo;
        
        NSNumber *prePreviousMonthDays = [self previousMonthDaysForPreviousDate:[_currentMonthDate previousMonthDate]];
        
        [self.monthArray removeAllObjects];
        [self.monthArray addObject:previousMonthInfo];
        [self.monthArray addObject:currentMothInfo];
        [self.monthArray addObject:nextMonthInfo];
        [self.monthArray addObject:prePreviousMonthDays];
        
    }
    // 向左滑动
    else if (scrollView.contentOffset.x > self.bounds.size.width) {
        
        _currentMonthDate = [_currentMonthDate nextMonthDate];
        NSDate *nextDate = [_currentMonthDate nextMonthDate];
        
        // 数组中最右边的月份现在作为中间的月份，中间的作为左边的月份，新的右边的需要重新获取
        GFCalendarMonth *previousMonthInfo = self.monthArray[1];
        GFCalendarMonth *currentMothInfo = self.monthArray[2];
        
        
        GFCalendarMonth *olderPreviousMonthInfo = self.monthArray[0];
        
        NSNumber *prePreviousMonthDays = [[NSNumber alloc] initWithInteger:olderPreviousMonthInfo.totalDays]; // 先保存 olderPreviousMonthInfo 的月天数
        
        // 复用 GFCalendarMonth 对象
        olderPreviousMonthInfo.totalDays = [nextDate totalDaysInMonth];
        olderPreviousMonthInfo.firstWeekday = [nextDate firstWeekDayInMonth];
        olderPreviousMonthInfo.year = [nextDate dateYear];
        olderPreviousMonthInfo.month = [nextDate dateMonth];
        GFCalendarMonth *nextMonthInfo = olderPreviousMonthInfo;

        
        [self.monthArray removeAllObjects];
        [self.monthArray addObject:previousMonthInfo];
        [self.monthArray addObject:currentMothInfo];
        [self.monthArray addObject:nextMonthInfo];
        [self.monthArray addObject:prePreviousMonthDays];
        
    }
    
    [_collectionViewM reloadData]; // 中间的 collectionView 先刷新数据
    [scrollView setContentOffset:CGPointMake(self.bounds.size.width, 0.0) animated:NO]; // 然后变换位置
    [_collectionViewL reloadData]; // 最后两边的 collectionView 也刷新数据
    [_collectionViewR reloadData];
    
    // 发通知，更改当前月份标题
    [self notifyToChangeCalendarHeader];
    
}

@end
